<?php

namespace App\Admin\Controllers;

use App\Models\Message;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class MessageController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('消息列表');
            $content->description('&nbsp;');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Message::class, function (Grid $grid) {

            $grid->model()->orderBy('CreateTime', 'desc');

            $grid->id('ID');
            $grid->user()->nickname('发送人');
            $grid->column('MsgType', '类型');
            $grid->column('CreateTime', '时间')->display(function($time) {
                return date("y-m-d H:i", $time);
            });
            $grid->column('内容')->display(function() {
                $json = json_decode($this->Content, true);
                $content = '未知消息';
                switch($this->MsgType) {
                    case 'text':
                        $content = $json['Content'];
                        break;
                    case 'event':
                        if(isset($json['Event'])) {
                            if(isset($json['EventKey'])) {
                                $content = $json['Event'] . ":" . $json['EventKey'];
                            } else {
                                $content = $json['Event'];
                            }
                        } else {
                            $content = '未知event';
                        }
                        break;
                    default:
                        $content = '=其他消息=';
                        break;
                }
                return $content;
            });
            //$grid->created_at();
            //$grid->updated_at();

            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableActions();

            $grid->filter(function($filter){
                $filter->disableIdFilter();
                $filter->equal('MsgType')->select([
                    'text' => 'text',
                    'event' => 'event',
                    'image' => 'image',
                    'voice' => 'voice',
                    'video' => 'video',
                    'location' => 'location',
                    'link' => 'link',
                    'file' => 'file',
                ]);
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Message::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
